package org.red5.websocket;

import org.red5.entity.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName WebsocketAPI
 * @Description //Websocket 推送消息接口
 * @Author 武玉锋
 * @Date 2019-07-25 16:46:
 **/
@Component
public class WebsocketAPI {

    @Autowired
    private SimpMessagingTemplate template;

    public void sendAlarmMessage(int code,String message) {
        //给在线用户发送通知
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,0, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>());
        executor.execute(new Runnable() {
            @Override
            public void run() {
//                if( sessionService.listOnlineUser().size()>0) {
                   org.red5.entity.Response response = new Response(code,message);
                    template.convertAndSend( "/queue/alarm", response);
//                }
            }
        });
        executor.shutdown();
    }

    public void sendMessageImm(int code, String message) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,0, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>());
        executor.execute(new Runnable() {
            @Override
            public void run() {
                    Response response = new Response(code,message);
                    template.convertAndSend( "/queue/dataImm", response);
            }
        });
        executor.shutdown();
    }
    public void sendEchartImm(int code, String message) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,0, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>());
        executor.execute(new Runnable() {
            @Override
            public void run() {
                    Response response = new Response(code,message);
                    template.convertAndSend( "/queue/echartImm", response);
            }
        });
        executor.shutdown();
    }


    public  void sendAllByObject(int code, Object message) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,0, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>());
        executor.execute(new Runnable() {
            @Override
            public void run() {
                Response response = new Response(code,message);
                template.convertAndSend( "/queue/alarm", response);
            }
        });
        executor.shutdown();
    }

}
